package com.meshnetics.gb.type;

/**
 * Data Type ID from ZCL.
 *
 * @author AKovylin
 */
public final class DataTypeID {
    private final byte dataTypeId;
    private final String className;
    private final String dataType;

    /**
     * Creates new DataTypeID.
     *
     * @param dataTypeId data type id (uint8 in ZCL)
     * @param className  name of type class
     * @param dataType   name of data type withing type class
     */
    public DataTypeID(byte dataTypeId, String className, String dataType) {
        this.dataTypeId = dataTypeId;
        this.className = className;
        this.dataType = dataType;
    }

    /**
     * Data Type ID is uint8.
     *
     * @return data type id
     */
    public byte getDataTypeId() {
        return dataTypeId;
    }

    /**
     * Returns most significant 5 bits as integer - type class.
     *
     * @return type class
     */
    public int getTypeClass() {
        return (dataTypeId >> 3) & 0x1f;
    }

    /**
     * Returns lest significant 3 bits as integer - data type within type class.
     *
     * @return data type withing type class
     */
    public int getDataTypeWithinClass() {
        return dataTypeId & 0x07;
    }

    /**
     * Returns name of type class in accordance with ZCL.
     *
     * @return name of type class
     */
    public String getTypeClassName() {
        return className;
    }

    /**
     * Returns name of data type withing type class in accordance with ZCL.
     *
     * @return name of data type withing type class
     */
    public String getDataTypeWithinClassName() {
        return dataType;
    }

    public boolean equals(Object obj) {
        return obj instanceof DataTypeID && this.dataTypeId == ((DataTypeID) obj).dataTypeId;
    }

    public int hashCode() {
        return dataTypeId;
    }

    public String toString() {
        String hex = Integer.toHexString(dataTypeId & 0xff);
        return "DataTypeID(0x" + (hex.length() == 1 ? ("0" + hex) : hex) + ',' + className + ',' + dataType + ')';
    }
}
